package com.cp.admin.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cp.admin.api.entity.vo.UserVO;
import com.cp.admin.core.common.constant.factory.ConstantFactory;
import com.cp.admin.core.datascope.DataScope;
import com.cp.admin.modular.system.dao.UserMapper;
import com.cp.admin.modular.system.model.Dept;
import com.cp.admin.modular.system.model.User;
import com.cp.admin.modular.system.service.IDeptService;
import com.cp.admin.modular.system.service.IUserService;
import com.cp.admin.modular.system.warpper.DeptUserVO;
import com.cp.core.node.ZTreeNode;
import com.cp.core.util.Convert;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-02-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IDeptService deptService;

    @Override
    public int setStatus(Integer userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int changePwd(Integer userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public List<Map<String, Object>> selectUsers(DataScope dataScope, String name, String beginTime, String endTime, Integer deptid) {
        return this.baseMapper.selectUsers(dataScope, name, beginTime, endTime, deptid);
    }

    @Override
    public int setRoles(Integer userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    @Override
    public User getByAccount(String account) {
        return this.baseMapper.getByAccount(account);
    }

    @Override
    public List<User> getUserByDept(Long deptId) {
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("deptid",deptId);
        wrapper.eq("status",1);
        return this.selectList(wrapper);
    }

    @Override
    public List<ZTreeNode> userDeptZTreeList() {
        //获取所有部门
        List<ZTreeNode> deptTreeList = deptService.tree();
        //循环取部门下的用户
        List<ZTreeNode> userDepteTreeList = new ArrayList<>();
        ZTreeNode zTreeNode = null;
        for(ZTreeNode deptTree:deptTreeList){
            //获取部门下的会员
            List<User> userList = getUserByDept(deptTree.getId());
            if(CollectionUtils.isNotEmpty(userList)){
                userDepteTreeList.add(deptTree);
                for(User user:userList){
                    zTreeNode = new ZTreeNode();
                    zTreeNode.setId(user.getId().longValue());
                    zTreeNode.setName(user.getName());
                    zTreeNode.setpId(deptTree.getId());
                    zTreeNode.setOpen(false);
                    userDepteTreeList.add(zTreeNode);
                }
            }
        }

        return userDepteTreeList;
    }

    @Override
    public Page<UserVO> searchUser(Integer pageNo, Integer pageSize, String searchKey) {
        Page<UserVO> page = new Page<>(pageNo,pageSize);
        page.setRecords(this.baseMapper.searchUser(page,searchKey));
        return page;
    }

    @Override
    public Page<UserVO> commonUser(Integer pageNo, Integer pageSize, Long userId) {
        Page<UserVO> page = new Page<>(pageNo,pageSize);
        page.setRecords(this.baseMapper.commonUser(page,userId));
        return page;
    }

    @Override
    public List<UserVO> commonUser(Long userId) {
        return this.baseMapper.commonUser(userId);
    }

    @Override
    public List<DeptUserVO> deptUserList(Long pId){
        List<DeptUserVO> deptUserList = new ArrayList<>();
        //查询一级部门
        List<Dept> deptList = deptService.getChildDeptList(pId);
        if(CollectionUtils.isNotEmpty(deptList)){
            DeptUserVO deptUser = null;
            for(Dept dept:deptList){
                //查询部门下的会员
                List<UserVO> userList = this.baseMapper.deptUserList(dept.getId());
                //查询子部门
                List<Dept> childList = deptService.getChildDeptList(dept.getId());
                //部门没有会员也没有子部门的不加入列表
                if(CollectionUtils.isEmpty(userList) && CollectionUtils.isEmpty(childList)){
                    continue;
                }
                deptUser = new DeptUserVO();
                deptUser.setDeptId(dept.getId());
                deptUser.setDeptName(dept.getFullname());
                deptUser.setPId(dept.getPid());
                if(CollectionUtils.isNotEmpty(userList)){
                    deptUser.setUserList(userList);
                }
                if(CollectionUtils.isNotEmpty(childList)){
                    //设置子部门
                    deptUser.setChildList(deptUserList(dept.getId()));
                }
                deptUserList.add(deptUser);
            }
        }
        return deptUserList;
    }


    @Override
    public DeptUserVO deptUserList() {
        DeptUserVO deptUserVO = new DeptUserVO();
        deptUserVO.setPId(0L);
        deptUserVO.setDeptId(0L);
        deptUserVO.setDeptName("公司");
        //获取顶级下的所有子部门信息
        List<DeptUserVO> deptUserList = deptUserList(0L);
        if(CollectionUtils.isNotEmpty(deptUserList)){
            deptUserVO.setChildList(deptUserList);
        }
        return deptUserVO;
    }

    @Override
    public boolean isScope(Long userId) {
        User user = this.selectById(userId);
        if(user != null){
            Integer[] roleArray = Convert.toIntArray(user.getRoleid());
            //当角色只有只等于6或7时
            if(roleArray.length == 1 && roleArray[0] == 6){
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public List<Long> getUserDataScope(Long userId) {
        User user = this.selectById(userId);
        if(user != null){
            Long deptId = user.getDeptid();
            List<Long> subDeptIds = ConstantFactory.me().getSubDeptId(deptId);
            subDeptIds.add(deptId);
            return subDeptIds;
        }
        return null;
    }
}
